<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    基于谷歌官方传感器示例代码的安卓手机传感器嵌入式实验过程 |  ShallCanyon&#39;s Blog
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/css/style.css">

  
<script src="/js/pace.min.js"></script>


  

  

</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-Android-accel-gyro-sensors-application-based-on-sample" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  基于谷歌官方传感器示例代码的安卓手机传感器嵌入式实验过程
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/04/Android-accel-gyro-sensors-application-based-on-sample/" class="article-date">
  <time datetime="2020-04-22T09:31:30.000Z" itemprop="datePublished">2020-04-22</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/Android/">Android</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.9k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">12分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <p>将近一个月前的嵌入式理论课，老师在结束时突然给了份实验作业——在AS上利用accelerometor（加速度传感器）和gyroscope（陀螺仪）做一个手势识别功能。这个手势识别定义非常模糊，不通过摄像头怎么判断用户的手势？后来上课老师才阐明所谓的手势就是拿手机做出挥手甚至写字的动作，并让软件来判断，而且还不给用第三方库，这就难到不少人了。不过后来老师也说本质上这个是为了让我们实现对传感器的利用，就算是只侦测到手机的移动也算，于是我将原本写一半的代码迅速补充了下，完成了老师的要求。</p>
<a id="more"></a>

<p>下面将是我的过程与理解：</p>
<h4 id="1-开始"><a href="#1-开始" class="headerlink" title="1. 开始"></a>1. 开始</h4><p>在上一次实验中，老师让我们运行谷歌的示例代码中的传感器代码，而这次的实验将基于这个示例代码进行改造。</p>
<p>谷歌样本代码下载： <a href="https://github.com/android/ndk-samples" target="_blank" rel="noopener">github链接</a> 或者从<a href="http://cloud.shallcanyon.top/s/z8t11zkr" target="_blank" rel="noopener">我的云盘</a>下载</p>
<h4 id="2-打开"><a href="#2-打开" class="headerlink" title="2. 打开"></a>2. 打开</h4><p>我们打开AS并打开样本中的sensor-graph（建议将这个项目单独移出来编写，保证原示例不动），打开后，我们得到以下的结果</p>
<p><img src="http://pic.shallcanyon.top/images/2020/04/22/image.png" alt="image.png"></p>
<p>AccelerometerGraphActivity是用来将数据描绘到软件主界面的，我们不需要，所以这个Activity之后可以不理</p>
<p>AccelerometerGraphJNI用来通过c++代码访问并处理速度传感器数据的NDK代码，我们要重点关注</p>
<p>sensorgraph.cpp就是访问并获得速度传感器的c++代码了，同样是要重点关注的内容</p>
<h4 id="3-加速度传感器"><a href="#3-加速度传感器" class="headerlink" title="3. 加速度传感器"></a>3. 加速度传感器</h4><h5 id="3-1-分析"><a href="#3-1-分析" class="headerlink" title="3.1 分析"></a>3.1 分析</h5><p>我们分析这个项目代码，最重要的是要搞清楚它是怎么读取到传感器数据的</p>
<p>通过查看JNI文件中几个native函数的源代码（在AS中，我们可以按住ctrl键并点击对应的函数名来访问这个函数的源代码），我们不难发现，项目获取速度传感器的数据来自于drawframe函数下的update函数，再查看update函数的源代码，我们可以看到如下结果：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">float</span> SENSOR_FILTER_ALPHA = <span class="number">0.1f</span>;</span><br><span class="line">...</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        ALooper_pollAll(<span class="number">0</span>, <span class="literal">NULL</span>, <span class="literal">NULL</span>, <span class="literal">NULL</span>);</span><br><span class="line">        ASensorEvent event;</span><br><span class="line">        <span class="keyword">float</span> a = SENSOR_FILTER_ALPHA;</span><br><span class="line">        <span class="keyword">while</span> (ASensorEventQueue_getEvents(accelerometerEventQueue, &amp;event, <span class="number">1</span>) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            sensorDataFilter.x = a * event.acceleration.x + (<span class="number">1.0f</span> - a) * sensorDataFilter.x;</span><br><span class="line">            sensorDataFilter.y = a * event.acceleration.y + (<span class="number">1.0f</span> - a) * sensorDataFilter.y;</span><br><span class="line">            sensorDataFilter.z = a * event.acceleration.z + (<span class="number">1.0f</span> - a) * sensorDataFilter.z;</span><br><span class="line">        &#125;</span><br><span class="line">        sensorData[sensorDataIndex] = sensorDataFilter;</span><br><span class="line">        sensorData[SENSOR_HISTORY_LENGTH+sensorDataIndex] = sensorDataFilter;</span><br><span class="line">        sensorDataIndex = (sensorDataIndex + <span class="number">1</span>) % SENSOR_HISTORY_LENGTH;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>其中重点的是while循环里的代码，如果我们去看<a href="https://developer.android.com/guide/topics/sensors/sensors_motion" target="_blank" rel="noopener">官方文档</a>， 可以发现循环体里与文档中计算加速度的代码非常相似：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onSensorChanged</span><span class="params">(SensorEvent event)</span></span>&#123;</span><br><span class="line">    <span class="comment">// In this example, alpha is calculated as t / (t + dT),</span></span><br><span class="line">    <span class="comment">// where t is the low-pass filter's time-constant and</span></span><br><span class="line">    <span class="comment">// dT is the event delivery rate.</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">final</span> <span class="keyword">float</span> alpha = <span class="number">0.8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Isolate the force of gravity with the low-pass filter.</span></span><br><span class="line">    gravity[<span class="number">0</span>] = alpha * gravity[<span class="number">0</span>] + (<span class="number">1</span> - alpha) * event.values[<span class="number">0</span>];</span><br><span class="line">    gravity[<span class="number">1</span>] = alpha * gravity[<span class="number">1</span>] + (<span class="number">1</span> - alpha) * event.values[<span class="number">1</span>];</span><br><span class="line">    gravity[<span class="number">2</span>] = alpha * gravity[<span class="number">2</span>] + (<span class="number">1</span> - alpha) * event.values[<span class="number">2</span>];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Remove the gravity contribution with the high-pass filter.</span></span><br><span class="line">    linear_acceleration[<span class="number">0</span>] = event.values[<span class="number">0</span>] - gravity[<span class="number">0</span>];</span><br><span class="line">    linear_acceleration[<span class="number">1</span>] = event.values[<span class="number">1</span>] - gravity[<span class="number">1</span>];</span><br><span class="line">    linear_acceleration[<span class="number">2</span>] = event.values[<span class="number">2</span>] - gravity[<span class="number">2</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>若有运行过项目代码，可以看到最终效果是手机调节方向时，实时描绘的曲线也跟着改变；从项目源代码可以看出，曲线的变化正是update函数中计算重力得出来的，那么根据文档的这段代码，我们可以轻易地写出侦测加速度的函数来</p>
<p>而传感器的数据怎么获得呢？可以看出，不论是项目源代码还是文档源代码，它们的传感器数据都来源于(A)SensorEvent，那么我们按住ctrl点击ASensorEvent，可以得到sensor.h中的结构体：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* <span class="doctag">NOTE:</span> changes to this struct has to be backward compatible */</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">ASensorEvent</span> &#123;</span></span><br><span class="line">    <span class="keyword">int32_t</span> version; <span class="comment">/* sizeof(struct ASensorEvent) */</span></span><br><span class="line">    <span class="keyword">int32_t</span> sensor;</span><br><span class="line">    <span class="keyword">int32_t</span> type;</span><br><span class="line">    <span class="keyword">int32_t</span> reserved0;</span><br><span class="line">    <span class="keyword">int64_t</span> timestamp;</span><br><span class="line">    <span class="keyword">union</span> &#123;</span><br><span class="line">        <span class="keyword">union</span> &#123;</span><br><span class="line">            <span class="keyword">float</span>           data[<span class="number">16</span>];</span><br><span class="line">            ASensorVector   <span class="built_in">vector</span>;</span><br><span class="line">            ASensorVector   acceleration;</span><br><span class="line">            ASensorVector   magnetic;</span><br><span class="line">            <span class="keyword">float</span>           temperature;</span><br><span class="line">            <span class="keyword">float</span>           distance;</span><br><span class="line">            <span class="keyword">float</span>           light;</span><br><span class="line">            <span class="keyword">float</span>           pressure;</span><br><span class="line">            <span class="keyword">float</span>           relative_humidity;</span><br><span class="line">            AUncalibratedEvent uncalibrated_gyro;</span><br><span class="line">            AUncalibratedEvent uncalibrated_magnetic;</span><br><span class="line">            AMetaDataEvent meta_data;</span><br><span class="line">            AHeartRateEvent heart_rate;</span><br><span class="line">            ADynamicSensorEvent dynamic_sensor_meta;</span><br><span class="line">            AAdditionalInfoEvent additional_info;</span><br><span class="line">        &#125;;</span><br><span class="line">        <span class="keyword">union</span> &#123;</span><br><span class="line">            <span class="keyword">uint64_t</span>        data[<span class="number">8</span>];</span><br><span class="line">            <span class="keyword">uint64_t</span>        step_counter;</span><br><span class="line">        &#125; u64;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">uint32_t</span> flags;</span><br><span class="line">    <span class="keyword">int32_t</span> reserved1[<span class="number">3</span>];</span><br><span class="line">&#125; ASensorEvent;</span><br></pre></td></tr></table></figure>

<p>可以看出，我们是访问这个结构体中的acceleration，而acceleration本身又是一个结构体ASensorVector，我们可以再次按住ctrl查看：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* <span class="doctag">NOTE:</span> changes to these structs have to be backward compatible */</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">ASensorVector</span> &#123;</span></span><br><span class="line">    <span class="keyword">union</span> &#123;</span><br><span class="line">        <span class="keyword">float</span> v[<span class="number">3</span>];</span><br><span class="line">        <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">            <span class="keyword">float</span> x;</span><br><span class="line">            <span class="keyword">float</span> y;</span><br><span class="line">            <span class="keyword">float</span> z;</span><br><span class="line">        &#125;;</span><br><span class="line">        <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">            <span class="keyword">float</span> azimuth;</span><br><span class="line">            <span class="keyword">float</span> pitch;</span><br><span class="line">            <span class="keyword">float</span> roll;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">int8_t</span> status;</span><br><span class="line">    <span class="keyword">uint8_t</span> reserved[<span class="number">3</span>];</span><br><span class="line">&#125; ASensorVector;</span><br></pre></td></tr></table></figure>

<p>一切简单明了了，我们通过访问ASensorEvent-&gt;acceleration-&gt;x, y, z即可获取加速度传感器的数据</p>
<h5 id="3-2-编写"><a href="#3-2-编写" class="headerlink" title="3.2. 编写"></a>3.2. 编写</h5><p>我们需要在sensorgraph.cpp中的sensorgraph类里添加新的数据成员：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">float</span> gravity[<span class="number">3</span>];</span><br><span class="line"><span class="keyword">float</span> linear_acceleration[<span class="number">3</span>];</span><br></pre></td></tr></table></figure>

<p>并增加一个计算加速度的函数：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">getAccel</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// In this example, alpha is calculated as t / (t + dT),</span></span><br><span class="line">        <span class="comment">// where t is the low-pass filter's time-constant and</span></span><br><span class="line">        <span class="comment">// dT is the event delivery rate.</span></span><br><span class="line">        ASensorEvent event;</span><br><span class="line">        <span class="keyword">float</span> alpha = <span class="number">0.8f</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Isolate the force of gravity with the low-pass filter.</span></span><br><span class="line">        <span class="keyword">while</span> (ASensorEventQueue_getEvents(accelerometerEventQueue, &amp;event, <span class="number">1</span>) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            gravity[<span class="number">0</span>] = alpha * gravity[<span class="number">0</span>] + (<span class="number">1</span> - alpha) * event.acceleration.x;</span><br><span class="line">            gravity[<span class="number">1</span>] = alpha * gravity[<span class="number">1</span>] + (<span class="number">1</span> - alpha) * event.acceleration.y;</span><br><span class="line">            gravity[<span class="number">2</span>] = alpha * gravity[<span class="number">2</span>] + (<span class="number">1</span> - alpha) * event.acceleration.z;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Remove the gravity contribution with the high-pass filter.</span></span><br><span class="line">            linear_acceleration[<span class="number">0</span>] = event.acceleration.x - gravity[<span class="number">0</span>];</span><br><span class="line">            linear_acceleration[<span class="number">1</span>] = event.acceleration.y - gravity[<span class="number">1</span>];</span><br><span class="line">            linear_acceleration[<span class="number">2</span>] = event.acceleration.z - gravity[<span class="number">2</span>];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>但数据都保存在我们的数据成员数组中，我们需要一个或多个native函数来获取所需的数据</p>
<p>在AccelerometerGraphJNI.java中我们加入以下几条函数：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="keyword">float</span> <span class="title">getAccelX</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="keyword">float</span> <span class="title">getAccelY</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="keyword">float</span> <span class="title">getAccelZ</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>

<p>并在sensorgraph.cpp底部新增以下函数：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">extern</span> <span class="string">"C"</span></span><br><span class="line">JNIEXPORT jfloat JNICALL</span><br><span class="line">Java_com_android_accelerometergraph_AccelerometerGraphJNI_getAccelX(JNIEnv *env, jclass clazz) &#123;</span><br><span class="line">    gSensorGraph.getAccel();</span><br><span class="line">    <span class="keyword">return</span> gSensorGraph.linear_acceleration[<span class="number">0</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="string">"C"</span></span><br><span class="line">JNIEXPORT jfloat JNICALL</span><br><span class="line">Java_com_android_accelerometergraph_AccelerometerGraphJNI_getAccelY(JNIEnv *env, jclass clazz) &#123;</span><br><span class="line">    gSensorGraph.getAccel();</span><br><span class="line">    <span class="keyword">return</span> gSensorGraph.linear_acceleration[<span class="number">1</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="string">"C"</span></span><br><span class="line">JNIEXPORT jfloat JNICALL</span><br><span class="line">Java_com_android_accelerometergraph_AccelerometerGraphJNI_getAccelZ(JNIEnv *env, jclass clazz) &#123;</span><br><span class="line">    gSensorGraph.getAccel();</span><br><span class="line">    <span class="keyword">return</span> gSensorGraph.linear_acceleration[<span class="number">2</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样，我们可以通过AccelerometerGraphJNI.getAccelX()来获取加速度传感器的x值了，y和z值同理</p>
<h4 id="4-陀螺仪传感器"><a href="#4-陀螺仪传感器" class="headerlink" title="4. 陀螺仪传感器"></a>4. 陀螺仪传感器</h4><h5 id="4-1-分析"><a href="#4-1-分析" class="headerlink" title="4.1 分析"></a>4.1 分析</h5><p>在ASensorEvent中我们除了需要的acceleration之外，还发现了uncalibrated_gyro，正是我们需要的陀螺仪传感器的结构体：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">AUncalibratedEvent</span> &#123;</span></span><br><span class="line">    <span class="keyword">union</span> &#123;</span><br><span class="line">        <span class="keyword">float</span> uncalib[<span class="number">3</span>];</span><br><span class="line">        <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">            <span class="keyword">float</span> x_uncalib;</span><br><span class="line">            <span class="keyword">float</span> y_uncalib;</span><br><span class="line">            <span class="keyword">float</span> z_uncalib;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="keyword">union</span> &#123;</span><br><span class="line">        <span class="keyword">float</span> bias[<span class="number">3</span>];</span><br><span class="line">        <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">            <span class="keyword">float</span> x_bias;</span><br><span class="line">            <span class="keyword">float</span> y_bias;</span><br><span class="line">            <span class="keyword">float</span> z_bias;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125; AUncalibratedEvent;</span><br></pre></td></tr></table></figure>

<p>uncalibrated指的是未校正的数据，这部分通过参考<a href="https://developer.android.com/guide/topics/sensors/sensors_motion" target="_blank" rel="noopener">官方文档</a>可以得到估算结果：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">uncalibrated_gyroscope_event.values[<span class="number">0</span>] - uncalibrated_gyroscope_event.values[<span class="number">3</span>]</span><br></pre></td></tr></table></figure>

<p>即 calibrated_x ~= uncalibrated_x - bias_estimate_x</p>
<p>这样看来，我们可以模仿加速度传感器那样添加代码了吗？还不止，我们重新看看加速度传感器部分的代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">getAccel</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// In this example, alpha is calculated as t / (t + dT),</span></span><br><span class="line">        <span class="comment">// where t is the low-pass filter's time-constant and</span></span><br><span class="line">        <span class="comment">// dT is the event delivery rate.</span></span><br><span class="line">        ASensorEvent event;</span><br><span class="line">        <span class="keyword">float</span> alpha = <span class="number">0.8f</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Isolate the force of gravity with the low-pass filter.</span></span><br><span class="line">        <span class="keyword">while</span> (ASensorEventQueue_getEvents(accelerometerEventQueue, &amp;event, <span class="number">1</span>) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            gravity[<span class="number">0</span>] = alpha * gravity[<span class="number">0</span>] + (<span class="number">1</span> - alpha) * event.acceleration.x;</span><br><span class="line">            gravity[<span class="number">1</span>] = alpha * gravity[<span class="number">1</span>] + (<span class="number">1</span> - alpha) * event.acceleration.y;</span><br><span class="line">            gravity[<span class="number">2</span>] = alpha * gravity[<span class="number">2</span>] + (<span class="number">1</span> - alpha) * event.acceleration.z;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// Remove the gravity contribution with the high-pass filter.</span></span><br><span class="line">            linear_acceleration[<span class="number">0</span>] = event.acceleration.x - gravity[<span class="number">0</span>];</span><br><span class="line">            linear_acceleration[<span class="number">1</span>] = event.acceleration.y - gravity[<span class="number">1</span>];</span><br><span class="line">            linear_acceleration[<span class="number">2</span>] = event.acceleration.z - gravity[<span class="number">2</span>];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>注意到这个while循环条件中的accelerometerEventQueue，如果我们要编写陀螺仪的代码，那这个条件势必要修改，那如果不加循环会如何呢？我一开始的时候就是没加循环的，结果最终结果完全不搭，和手机的移动没有任何关联，添加循环后就正常了，所以这个循环体是关键</p>
<p>那么我们查看下这个accelerometerEventQueue到底是什么：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">sensorgraph</span> &#123;</span></span><br><span class="line">    ...</span><br><span class="line">    ASensorManager *sensorManager;</span><br><span class="line">    <span class="keyword">const</span> ASensor *accelerometer;</span><br><span class="line">    ASensorEventQueue *accelerometerEventQueue;</span><br><span class="line">    ALooper *looper;</span><br><span class="line">    ...</span><br></pre></td></tr></table></figure>

<p>可以看到，sensorgraph类定义了这几个处理加速度传感器的数据成员，我们往下看看这几个成员被做了什么处理。</p>
<p>在void init(AAssetManager *assetManager)函数中，各种数据得到了初始化，其中不乏我们要找的这几个成员：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">sensorManager = AcquireASensorManagerInstance();</span><br><span class="line">assert(sensorManager != <span class="literal">NULL</span>);</span><br><span class="line">accelerometer = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_ACCELEROMETER);</span><br><span class="line">assert(accelerometer != <span class="literal">NULL</span>);</span><br><span class="line">looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);</span><br><span class="line">assert(looper != <span class="literal">NULL</span>);</span><br><span class="line">accelerometerEventQueue = ASensorManager_createEventQueue(sensorManager, looper,</span><br><span class="line">                                                                  LOOPER_ID_USER, <span class="literal">NULL</span>, <span class="literal">NULL</span>);</span><br><span class="line">assert(accelerometerEventQueue != <span class="literal">NULL</span>);</span><br><span class="line"><span class="keyword">auto</span> status = ASensorEventQueue_enableSensor(accelerometerEventQueue,</span><br><span class="line">                                                     accelerometer);</span><br><span class="line">assert(status &gt;= <span class="number">0</span>);</span><br><span class="line">status = ASensorEventQueue_setEventRate(accelerometerEventQueue,</span><br><span class="line">                                                accelerometer,</span><br><span class="line">                                                SENSOR_REFRESH_PERIOD_US);</span><br><span class="line">assert(status &gt;= <span class="number">0</span>);</span><br><span class="line">(<span class="keyword">void</span>)status;   <span class="comment">//to silent unused compiler warning</span></span><br></pre></td></tr></table></figure>

<p>因为这个项目代码目标仅仅是使用加速度传感器，所以，为了能够读取到陀螺仪传感器的数据，我们需要仿照着写一些新代码。</p>
<h5 id="4-2-编写"><a href="#4-2-编写" class="headerlink" title="4.2 编写"></a>4.2 编写</h5><p>首先，我们要在sensorgraph类里添加新的数据成员：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> ASensor *gyroscope;</span><br><span class="line">ASensorEventQueue *gyroscopeEventQueue;</span><br><span class="line">ALooper *gyroLooper;</span><br></pre></td></tr></table></figure>

<p>这里的Looper我不清楚有什么用，但是为了防止与加速度传感器的定义重合，我也添加了gyro独有的Looper</p>
<p>然后，我们在void init(AAssetManager *assetManager)函数底下添加陀螺仪初始化代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">sensorManager = AcquireASensorManagerInstance();</span><br><span class="line">assert(sensorManager != <span class="literal">NULL</span>);</span><br><span class="line">gyroscope = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_GYROSCOPE);</span><br><span class="line">assert(gyroscope != <span class="literal">NULL</span>);</span><br><span class="line">gyroLooper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);</span><br><span class="line">assert(gyroLooper != <span class="literal">NULL</span>);</span><br><span class="line">gyroscopeEventQueue = ASensorManager_createEventQueue(sensorManager, gyroLooper,</span><br><span class="line">                                                          LOOPER_ID_USER, <span class="literal">NULL</span>, <span class="literal">NULL</span>);</span><br><span class="line">assert(gyroscopeEventQueue != <span class="literal">NULL</span>);</span><br><span class="line"><span class="keyword">auto</span> gyroStatus = ASensorEventQueue_enableSensor(gyroscopeEventQueue,</span><br><span class="line">                                                 gyroscope);</span><br><span class="line">assert(gyroStatus &gt;= <span class="number">0</span>);</span><br><span class="line">gyroStatus = ASensorEventQueue_setEventRate(gyroscopeEventQueue,</span><br><span class="line">                                            gyroscope,</span><br><span class="line">                                        SENSOR_REFRESH_PERIOD_US);</span><br><span class="line">assert(gyroStatus &gt;= <span class="number">0</span>);</span><br><span class="line">(<span class="keyword">void</span>)gyroStatus;   <span class="comment">//to silent unused compiler warning</span></span><br></pre></td></tr></table></figure>

<p>注意将ASensorManager_getDefaultSensor的第二个参数改成ASENSOR_TYPE_GYROSCOPE</p>
<p>这样，我们就可以仿照之前添加的加速度传感器代码来添加陀螺仪传感器代码了：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line"><span class="keyword">float</span> gyro[<span class="number">3</span>];</span><br><span class="line">...</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">getGyro</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        ASensorEvent event;</span><br><span class="line">        <span class="keyword">while</span> (ASensorEventQueue_getEvents(gyroscopeEventQueue, &amp;event, <span class="number">1</span>) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            gyro[<span class="number">0</span>] = event.uncalibrated_gyro.x_uncalib - event.uncalibrated_gyro.x_bias;</span><br><span class="line">            gyro[<span class="number">1</span>] = event.uncalibrated_gyro.y_uncalib - event.uncalibrated_gyro.y_bias;</span><br><span class="line">            gyro[<span class="number">2</span>] = event.uncalibrated_gyro.z_uncalib - event.uncalibrated_gyro.z_bias;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>再在AccelerometerGraphJNI.java中添加新的native函数：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="keyword">float</span> <span class="title">getGyroX</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="keyword">float</span> <span class="title">getGyroY</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="keyword">float</span> <span class="title">getGyroZ</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>

<p>并在sensorgraph.cpp中添加对应的函数实现：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">extern</span> <span class="string">"C"</span></span><br><span class="line">JNIEXPORT jfloat JNICALL</span><br><span class="line">Java_com_android_accelerometergraph_AccelerometerGraphJNI_getGyroX(JNIEnv *env, jclass clazz) &#123;</span><br><span class="line">    gSensorGraph.getGyro();</span><br><span class="line">    <span class="keyword">return</span> gSensorGraph.gyro[<span class="number">0</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="string">"C"</span></span><br><span class="line">JNIEXPORT jfloat JNICALL</span><br><span class="line">Java_com_android_accelerometergraph_AccelerometerGraphJNI_getGyroY(JNIEnv *env, jclass clazz) &#123;</span><br><span class="line">    gSensorGraph.getGyro();</span><br><span class="line">    <span class="keyword">return</span> gSensorGraph.gyro[<span class="number">1</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="string">"C"</span></span><br><span class="line">JNIEXPORT jfloat JNICALL</span><br><span class="line">Java_com_android_accelerometergraph_AccelerometerGraphJNI_getGyroZ(JNIEnv *env, jclass clazz) &#123;</span><br><span class="line">    gSensorGraph.getGyro();</span><br><span class="line">    <span class="keyword">return</span> gSensorGraph.gyro[<span class="number">2</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样，我们就可以通过AccelerometerGraphJNI.getGyroX()来获得陀螺仪x轴数据了，y与z轴数据同理</p>
<h4 id="5-功能展示"><a href="#5-功能展示" class="headerlink" title="5. 功能展示"></a>5. 功能展示</h4><p>既然能获取数据了，那我们得想办法把数据展现在我们面前，我的想法是创建个UI，把2个传感器的数据显示在上面，并根据这些数据来判断手机的运动</p>
<p>首先，我们需要创建一个新的Activity</p>
<p><img src="http://pic.shallcanyon.top/images/2020/04/22/imageb20098d2430e3deb.png" alt="imageb20098d2430e3deb.png"></p>
<p>然后在AndroidManifest.xml中把我们新创建的Activity设置为主Activity</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">manifest</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">package</span>=<span class="string">"com.android.accelerometergraph"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">uses-feature</span> <span class="attr">android:glEsVersion</span>=<span class="string">"0x00020000"</span> <span class="attr">android:required</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">application</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:allowBackup</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:fullBackupContent</span>=<span class="string">"false"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:icon</span>=<span class="string">"@mipmap/ic_launcher"</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:label</span>=<span class="string">"@string/accelerometergraph_activity"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">activity</span> <span class="attr">android:name</span>=<span class="string">".MainActivity"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:theme</span>=<span class="string">"@style/Theme.AppCompat.Light"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:launchMode</span>=<span class="string">"singleTask"</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:configChanges</span>=<span class="string">"orientation|keyboardHidden"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">action</span> <span class="attr">android:name</span>=<span class="string">"android.intent.action.MAIN"</span> /&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">category</span> <span class="attr">android:name</span>=<span class="string">"android.intent.category.LAUNCHER"</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">activity</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">activity</span> <span class="attr">android:name</span>=<span class="string">".AccelerometerGraphActivity"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">application</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">manifest</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>注意这里谷歌的源代码的AndroidManifest.xml中是</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">android:theme="@android:style/Theme.NoTitleBar.Fullscreen</span><br></pre></td></tr></table></figure>

<p>如果我们的AS版本比较新的话那新创建的Activity是继承的AppCompatActivity，如果不把theme改为</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">android:theme="@style/Theme.AppCompat"</span><br></pre></td></tr></table></figure>

<p>或者其子主题的话，那程序打开即崩溃，具体可以自行百度</p>
<p>接下来怎么编写UI和功能代码，以及如何处理数据，这就得看自己的发挥了</p>
<p><strong>我的结果：</strong></p>
<p><a href="http://pic.shallcanyon.top/image/LhF" target="_blank" rel="noopener"><img src="http://pic.shallcanyon.top/images/2020/04/22/image216857f7da861b9d.png" alt="image216857f7da861b9d.png"></a></p>
<h4 id="6-补充"><a href="#6-补充" class="headerlink" title="6. 补充"></a>6. 补充</h4><h5 id="6-1-手机对应的三维轴"><a href="#6-1-手机对应的三维轴" class="headerlink" title="6.1 手机对应的三维轴"></a>6.1 手机对应的三维轴</h5><p>如图所示，对加速度和陀螺仪传感器都受用</p>
<p><a href="http://pic.shallcanyon.top/image/jW0" target="_blank" rel="noopener"><img src="http://pic.shallcanyon.top/images/2020/04/22/image1975bb4b40b6ef86.png" alt="image1975bb4b40b6ef86.png"></a></p>
<h5 id="6-2-多线程下的UI更新"><a href="#6-2-多线程下的UI更新" class="headerlink" title="6.2 多线程下的UI更新"></a>6.2 多线程下的UI更新</h5><p>为了保证UI线程安全，AS禁止子线程对主线程UI的更新操作，而为了能够让子线程处理的过程中能更新UI，可以通过定义Handler和Message来让子线程发送消息给主线程进行UI的更新</p>
<h5 id="6-3-源代码分享"><a href="#6-3-源代码分享" class="headerlink" title="6.3 源代码分享"></a>6.3 源代码分享</h5><p>可以通过<a href="cloud.shallcanyon.top/s/54g8jl8c">下载链接</a>下载我的源项目，若有同上嵌入式的同学借鉴的话记得做修改:^</p>

      
      <!-- 打赏 -->
      
    </div>
    <footer class="article-footer">
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Android/" rel="tag">Android</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/NDK/" rel="tag">NDK</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/embedded/" rel="tag">embedded</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/sensor/" rel="tag">sensor</a></li></ul>

    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/06/open-local-jsp-files-in-explorer/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            如何用浏览器运行jsp文件
          
        </div>
      </a>
    
    
      <a href="/2020/04/yum-bug-after-pyUpgrade/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">关于CentOS更新python后导致yum出错的原因与修补方法</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: '',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        
          Powered by
        
        
        <a href="https://hexo.io" target="_blank">Hexo</a> Theme <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <ul class="list-inline">
  <li>PV:<span id="busuanzi_value_page_pv"></span></li>
  <li>UV:<span id="busuanzi_value_site_uv"></span></li>
</ul>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
	<ul class="list-inline">
		<li>
			© 2009-2020 Aliyun.com 版权所有 ICP证：
			<a href="http://www.beian.miit.gov.cn" target="_blank" rel="noopener">粤ICP备19103977号</a>
		</li>
	</ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="ShallCanyon&#39;s Blog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>



<script src="/fancybox/jquery.fancybox.min.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['3, 2, 1 RIDER KICK','',''],
    startDelay: 0,
    typeSpeed: 200,
    loop: true,
    backSpeed: 100,
    showCursor: false
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: true
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>



<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>